package com.fruit.utils;

import java.util.List;

import java.util.Map;

import javax.faces.application.FacesMessage;
import javax.faces.model.SelectItem;

import com.fruit.service.ICityService;
import com.fruit.service.ICountyService;
import com.fruit.service.IProvinceService;
import com.fruit.web.bo.impl.City;
import com.fruit.web.bo.impl.County;
import com.fruit.web.bo.impl.Province;
import com.fruit.web.constant.EjdConstants;
import com.fruit.web.constant.SpringBeanConstants;


public final class RegionUtil {
	public RegionUtil() {
	  }
	public static SelectItem[] getProvinceItems() {
		SelectItem[] inProvinceItems;
		IProvinceService provinceService = (IProvinceService)SpringFacesUtil.getManagedBean(SpringBeanConstants.PROVINCE_SERVICE_BEAN_NAME);
		provinceService.setEntityClass(Province.class);
		provinceService.setPKClass(Long.class);
		List<Province> provinces = provinceService.getAll();
		if (null == provinces) {
			inProvinceItems =  new SelectItem[1];
		} else {
			inProvinceItems = new SelectItem[provinces.size() + 1];
		}
		String provinceSelectNameN18 = MessageFactory.getMessage(EjdConstants.USER_BUNDLE_NAME,"province.select.name", FacesMessage.SEVERITY_ERROR).getDetail();
		inProvinceItems[0]= new SelectItem(EjdConstants.BLANK_VALUE_SELECT_ITEM,provinceSelectNameN18);
		if (null != provinces && provinces.size() > 1) {
			 for (int i = 0 ; i < provinces.size(); i++) {
				 Province tempProvince = provinces.get(i);
				 inProvinceItems[i + 1] = new SelectItem(tempProvince.getId().toString(),tempProvince.getName());
			 }
		}
		return inProvinceItems;
		//province Items set end
		//start set city items
		
	}
	
	
	public static SelectItem[] getCityItems(String province) {
		
		//start set city items
		
		SelectItem[] inCityItems;
		ICityService cityService = (ICityService)SpringFacesUtil.getManagedBean(SpringBeanConstants.CITY_SERVICE_BEAN_NAME);
		cityService.setEntityClass(City.class);
		cityService.setPKClass(Long.class);
		List<City> cities = null;
		if (null != province && !("".equals(province))) {
			cities = cityService.findBy("provinceId", new Long(province));
		}
		if (null == cities) {
			inCityItems =  new SelectItem[1];
		} else {
			inCityItems = new SelectItem[cities.size() + 1];
		}
		String citySelectNameN18 = MessageFactory.getMessage(EjdConstants.USER_BUNDLE_NAME,"city.select.name", FacesMessage.SEVERITY_ERROR).getDetail();
		inCityItems[0]= new SelectItem(EjdConstants.BLANK_VALUE_SELECT_ITEM,citySelectNameN18);
		if (null != cities && cities.size() > 1) {
			 for (int i = 0 ; i < cities.size(); i++) {
				 City tempCity = cities.get(i);
				 inCityItems[i + 1] = new SelectItem(tempCity.getId().toString(),tempCity.getName());
			 }
		}
		return inCityItems;
		//end
	}
	
	
	public static SelectItem[] getCountyItems(String city) {
		
		//end
		//start set county items
		SelectItem[] inCountyItems;
		ICountyService countyService = (ICountyService)SpringFacesUtil.getManagedBean(SpringBeanConstants.COUNTY_SERVICE_BEAN_NAME);
		countyService.setEntityClass(County.class);
		countyService.setPKClass(Long.class);
		List<County> counties = null;
		if (null != city && !("".equals(city))) {
			counties = countyService.findBy("cityId", new Long(city));
		}
		if (null == counties) {
			inCountyItems =  new SelectItem[1];
		} else {
			inCountyItems = new SelectItem[counties.size() + 1];
		}
		String countySelectNameN18 = MessageFactory.getMessage(EjdConstants.USER_BUNDLE_NAME,"county.select.name", FacesMessage.SEVERITY_ERROR).getDetail();
		inCountyItems[0]= new SelectItem(EjdConstants.BLANK_VALUE_SELECT_ITEM,countySelectNameN18);
		if (null != counties && counties.size() > 1) {
			 for (int i = 0 ; i < counties.size(); i++) {
				 County tempCounty = counties.get(i);
				 inCountyItems[i + 1] = new SelectItem(tempCounty.getId().toString(),tempCounty.getName());
			 }
		}
		return inCountyItems;
		//end
		//start set county items
	}

}
